package com.mall.action;

import com.github.pagehelper.PageInfo;
import com.mall.bean.QueryWhereVO;
import com.mall.bean.User;
import com.mall.bean.UserRole;
import com.mall.service.UserService;
import com.mall.uitl.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lwd1513
 * @date 2022-04-25 16 14
 * discription
 */
@RestController
@RequestMapping("UserController2")
public class UserController2 {

    Logger logger = LoggerFactory.getLogger(this.getClass());//ExceptionInterceptor

    @Autowired
    private UserService userService;

    @RequestMapping("query")
    public JsonUtil query(QueryWhereVO queryWhereVO, @RequestParam("username") String username) {

        User user = new User();

        user.setUsername(username);

        String currentPage = queryWhereVO.getCurrentPage();
        String pageSize = queryWhereVO.getPageSize();

        PageInfo<User> pageInfo = userService.queryDataByPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize), user);

        JsonUtil jsonUtil = new JsonUtil();
        jsonUtil.setData(pageInfo);

        return jsonUtil;
    }

    //updateUserStateExChange
    @RequestMapping("updateUserStateExChange")
    public JsonUtil updateUserStateExChange(
            String flag,
            @RequestParam("user_id") String userId) {

        JsonUtil jsonUtil = new JsonUtil();
        int res = userService.updateUserStateExChange(Integer.parseInt(flag), Integer.parseInt(userId));
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }

    //saveUser
    @RequestMapping("saveUser")
    public JsonUtil saveUser(User user) {

        logger.debug(user.toString());
        int res = userService.insert(user);

        JsonUtil jsonUtil = new JsonUtil();

        jsonUtil.setMsg("成功！！！");
        return jsonUtil;
    }

    //updatePathByUserId
    @RequestMapping("updatePathByUserId")
    public JsonUtil updatePathByUserId(String file,String username) {

        JsonUtil jsonUtil = new JsonUtil();
        int res = userService.updatePathByUserId(file, username);
        jsonUtil.setCode(0);
        jsonUtil.setMsg("成功");

        return jsonUtil;
    }


    //updateUserById
    @RequestMapping("updateUserById")
    public JsonUtil updateUserById(User user) {

        JsonUtil jsonUtil = new JsonUtil();
        int res = userService.updateUserById(user);
        jsonUtil.setCode(0);
        jsonUtil.setMsg("成功");
        return jsonUtil;
    }

    //deleteUserById
    @RequestMapping("deleteUserById/{userId}")
    public JsonUtil deleteUserById(@PathVariable("userId") String userId) {
        JsonUtil jsonUtil = new JsonUtil();

        int res = userService.deleteById(userId);
        jsonUtil.setMsg("删除成功");

        return jsonUtil;
    }
    //assignRole
    @RequestMapping("assignRole")
    public JsonUtil assignRole(String role_id,String user_id) {

        UserRole userRole = new UserRole();
        userRole.setRole_id(Integer.parseInt(role_id));
        JsonUtil jsonUtil = new JsonUtil();

        userRole.setUser_id(Integer.parseInt(user_id));

        int res = userService.assignRoleById(userRole);
        jsonUtil.setMsg("添加角色成功");

        return jsonUtil;
    }

}
